# coding: utf8


class ProvinceNode:
	def __init__(self, index, name, weight, min_limit, max_limit):
		self.index = index
		self.name = name
		self.weight = weight
		self.min_limit = min_limit
		self.max_limit = max_limit

	def __contains__(self, ratio):
		return self.min_limit <= ratio <= self.max_limit


class ProvinceData:
	PROVINCE_HEBEI = 0
	PROVINCE_SHAN1XI = 1
	PROVINCE_LIAONING = 2
	PROVINCE_JILING = 3
	PROVINCE_HEILONGJIANG = 4
	PROVINCE_JIANGSU = 5
	PROVINCE_ZHEJIANG = 6
	PROVINCE_ANHUI = 7
	PROVINCE_FUJIAN = 8
	PROVINCE_JIANGXI = 9
	PROVINCE_SHANDONG = 10
	PROVINCE_HENAN = 11
	PROVINCE_HUBEI = 12
	PROVINCE_HUNAN = 13
	PROVINCE_GUANGDONG = 14
	PROVINCE_HAINAN = 15
	PROVINCE_SICHUAN = 16
	PROVINCE_GUIZHOU = 17
	PROVINCE_YUNNAN = 18
	PROVINCE_SHAN3XI = 19
	PROVINCE_GANSU = 20
	PROVINCE_QINGHAI = 21
	PROVINCE_TAIWAN = 22
	PROVINCE_NEIMENGGU = 23
	PROVINCE_GUANGXI = 24
	PROVINCE_XIZANG = 25
	PROVINCE_NINGXIA = 26
	PROVINCE_XINJIANG = 27
	PROVINCE_XIANGGANG = 28
	PROVINCE_AOMENG = 29
	PROVINCE_BEIJING = 30
	PROVINCE_SHANGHAI = 31
	PROVINCE_TIANJING = 32
	PROVINCE_CHONGQING = 33

	PROVINCE_DATA = {
		PROVINCE_HEBEI: ('河北省', 30),
		PROVINCE_SHAN1XI: ('山西省', 0),
		PROVINCE_LIAONING: ('辽宁省', 0),
		PROVINCE_JILING: ('吉林省', 0),
		PROVINCE_HEILONGJIANG: ('黑龙江省', 0),
		PROVINCE_JIANGSU: ('江苏省', 150),
		PROVINCE_ZHEJIANG: ('浙江省', 300),
		PROVINCE_ANHUI: ('安徽省', 90),
		PROVINCE_FUJIAN: ('福建省', 0),
		PROVINCE_JIANGXI: ('江西省', 0),
		PROVINCE_SHANDONG: ('山东省', 0),
		PROVINCE_HENAN: ('河南省', 30),
		PROVINCE_HUBEI: ('湖北省', 0),
		PROVINCE_HUNAN: ('湖南省', 0),
		PROVINCE_GUANGDONG: ('广东省', 230),
		PROVINCE_HAINAN: ('海南省', 0),
		PROVINCE_SICHUAN: ('四川省', 80),
		PROVINCE_GUIZHOU: ('贵州省', 60),
		PROVINCE_YUNNAN: ('云南省', 0),
		PROVINCE_SHAN3XI: ('陕西省', 10),
		PROVINCE_GANSU: ('甘肃省', 0),
		PROVINCE_QINGHAI: ('青海省', 0),
		PROVINCE_TAIWAN: ('台湾省', 0),
		PROVINCE_NEIMENGGU: ('内蒙古自治区', 0),
		PROVINCE_GUANGXI: ('广西壮族自治区', 10),
		PROVINCE_XIZANG: ('西藏自治区', 0),
		PROVINCE_NINGXIA: ('宁夏回族自治区', 0),
		PROVINCE_XINJIANG: ('新疆维吾尔自治区', 0),
		PROVINCE_XIANGGANG: ('香港特别行政区', 0),
		PROVINCE_AOMENG: ('澳门特别行政区', 0),
		PROVINCE_SHANGHAI: ('上海', 10),
		PROVINCE_BEIJING: ('北京', 3),
		PROVINCE_TIANJING: ('天津', 0),
		PROVINCE_CHONGQING: ('重庆', 0),
	}

	RATIO_TOTAL = 10000.0

	def items(self):
		"""
		:return: choice 列表
		"""
		titems = []
		for k, v in self.PROVINCE_DATA.items():
			titems.append((k, v[0]))
		return titems

	def __init__(self):
		keys = sorted(self.PROVINCE_DATA.keys())

		self.provinces = []
		self.total_weight = 0

		for key in keys:
			name = self.PROVINCE_DATA[key][0]
			weight = self.PROVINCE_DATA[key][1]
			self.provinces.append(ProvinceNode(key, name, weight, self.total_weight, self.total_weight + weight))
			self.total_weight += weight

	def get_province(self, ratio):
		"""
		由随机因子获得省对象
		:param ratio: 1 ~ RATIO_TOTAL 的随机整数
		:return: 省对象
		:rtype: ProvinceNode
		"""
		if 1 <= ratio <= self.RATIO_TOTAL:
			index = int(self.total_weight * (ratio / self.RATIO_TOTAL))
			for node in self.provinces:
				if index in node:
					return node
		else:
			raise ValueError('ratio should be in range [1, %s], but given is %s' % (self.RATIO_TOTAL, ratio))

	def get_province_random(self):
		"""
		随机获取省对象，获取的概率由省对象权重决定
		:return: 省对象
		:rtype: ProvinceNode
		"""
		import random
		return self.get_province(random.randint(1, int(self.RATIO_TOTAL)))

